/**
 * Created by mama on 2016/3/31.
 */
/**
 * tabs jquery plugins
 */
/*
 <ul class="tab-control">
 <li data-tab="#i"></li>
 <li data-tab="#j"></li>
 </ul>

 <div id="#i"></div>
 <div id="#j"></div>
 */
define(function () {
    (function ($) {
        $.fn.tab = function (options) {
            var defaults = {
                    index: '0',
                    controlShowClass: "active",
                    contentShowClass: 'active',
                    callback: function () {
                    }
                },
                self = this,
                privates = {};

            options = $.extend({}, defaults, options);

            var $control = self.find('.tab-control'),
                $controlItem = $control.children('li');

            function bulidEvent() {
                $control.on('click', 'li', function () {
                    var $this = $(this);

                    $this.addClass(options.controlShowClass);
                    $(this.getAttribute('data-tab')).addClass(options.contentShowClass);

                    var dom = $this.siblings('.' + options.controlShowClass);
                    $(dom.attr('data-tab')).removeClass(options.contentShowClass);
                    dom.removeClass(options.controlShowClass);

                    options.callback(this);
                });
            }

            self.to = function (index) {
                if (index) {
                    index = index < 0 ? 0 : index;
                    $controlItem.eq(index).trigger('click');
                } else {
                    $controlItem.eq(0).trigger('click');
                }
                return this;
            };

            var init = function () {
                bulidEvent();
                self.to(options.index);
            };

            init();

            return self;
        };
    })(jQuery);
    return jQuery;
});
